package com.xblog.chatbox.vlmp.core.dify;



import com.xblog.chatbox.vlmp.core.VlmpAgentChatClient;
import com.xblog.chatbox.vlmp.core.dify.agent.DifyChatAgent;
import com.xblog.chatbox.vlmp.core.dify.agent.DifyChatAgentClient;
import okhttp3.OkHttpClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @author wupy
 **/
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(prefix = "spring.ai.agent", name = "provider", havingValue = "dify")
@EnableConfigurationProperties({DifyProperties.class, DifyChatAgent.class})
public class DifyAgentConfiguration {

    @Bean
    @ConditionalOnMissingBean(OkHttpClient.class)
    public OkHttpClient okHttpClient(DifyProperties properties) {
        return new OkHttpClient.Builder()
                .callTimeout(Optional.ofNullable(properties.getConnectTimeout()).orElse(120L), TimeUnit.SECONDS)
                .readTimeout(Optional.ofNullable(properties.getSocketTimeout()).orElse(120L), TimeUnit.SECONDS)
                .build();
    }

    @Bean
    @ConditionalOnMissingBean(VlmpAgentChatClient.class)
    @ConditionalOnProperty(prefix = "spring.ai.agent.dify.chat", name = "enabled", havingValue = "true", matchIfMissing = true)
    public VlmpAgentChatClient hazbotChatClient(OkHttpClient okHttpClient, DifyProperties properties, DifyChatAgent agent) {
        return new DifyChatAgentClient(okHttpClient, properties, agent);
    }
}
